<template>
	<a-modal
		:title="title"
		:width="modalWidth"
		:bodyStyle="bodyStyle"
		:visible="visible"
		:confirmLoading="confirmLoading"
		@ok="handleOk"
		@cancel="handleCancel"
		:destroyOnClose="true"
		:maskClosable="false"
	>
		<a-spin :spinning="confirmLoading">
			<a-form :form="form" autocomplete="off">
				<a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="账户">
					<a-input
						placeholder="账户"
						:disabled="!isAdd"
						v-decorator="[
							'account',
							{
								rules: [{ required: true, whitespace: true, message: '请输入账户' }],
								getValueFromEvent: removeSpaces
							}
						]"
					/>
				</a-form-item>
				<a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="昵称">
					<a-input
						placeholder="昵称"
						v-decorator="[
							'name',
							{
								rules: [{ required: true, whitespace: true, message: '请输入昵称' }],
								getValueFromEvent: removeSpaces,
								trigger: 'blur'
							}
						]"
					/>
				</a-form-item>
				<a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="密码">
					<a-input-password
						class="w100"
						placeholder="密码"
						:visibilityToggle="false"
						v-decorator="[
							'password',
							{
								rules: [{ required: isAdd, message: '请输入密码' }, { validator: validateToNextPassword }]
							}
						]"
					/>
				</a-form-item>
				<a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="确认密码">
					<a-input-password
						class="w100"
						placeholder="确认密码"
						:visibilityToggle="false"
						@blur="handleConfirmBlur"
						v-decorator="[
							'confirmPassWord',
							{
								rules: [{ required: isAdd, message: '请输入确认密码' }, { validator: compareToFirstPassword }]
							}
						]"
					/>
				</a-form-item>
			</a-form>
		</a-spin>
	</a-modal>
</template>

<script>
import { api } from '@/api/index.js';
import { ModalMixins } from '@/mixins';
export default {
	name: 'addConfigModal',
	mixins: [ModalMixins],
	data() {
		return {
			confirmDirty: false
		};
	},
	created() {},
	methods: {
		add(record) {
			this.visible = true;
			if (record) {
				this.title = '编辑用户';
				this.isAdd = false;
				this.mdl = { ...record };
				this.init();
			} else {
				this.title = '新增用户';
				this.isAdd = true;
			}
		},
		init() {
			this.$nextTick(() => {
				this.form.setFieldsValue(this.$pick(this.mdl, 'account', 'name'));
			});
		},
		close() {
			this.$emit('close');
			this.visible = false;
		},
		handleOk() {
			const _this = this;
			// 触发表单验证
			this.form.validateFields((err, values) => {
				// 验证表单没错误
				if (!err) {
					let parameter = { ...values };
					/* 判断一下密码 */
					if (parameter.password != parameter.confirmPassWord) {
						_this.$message.error('两次输入的密码不匹配');
						return false;
					}
					let { account, name, password } = parameter;
					_this.confirmLoading = true;
					this.$postAction(
						api.saveOrUpdateUser,
						Object.assign(
							{
								account,
								name,
								password
							},
							!this.isAdd ? { id: this.mdl.id } : null
						)
					)
						.then(res => {
							if (res.code == 200) {
								_this.$message.success('保存成功');
								_this.$emit('ok');
								_this.form.resetFields();
								_this.visible = false;
							}
						})
						.finally(() => {
							_this.confirmLoading = false;
						});
				}
			});
		},
		handleCancel() {
			this.close();
		},
		compareToFirstPassword(rule, value, callback) {
			const form = this.form;
			if (value && value !== form.getFieldValue('password')) {
				callback('两次输入的密码不匹配');
			} else {
				callback();
			}
		},
		validateToNextPassword(rule, value, callback) {
			const form = this.form;
			if (value && this.confirmDirty) {
				form.validateFields(['confirmPassWord'], {
					force: true
				});
			}
			callback();
		},
		handleConfirmBlur(e) {
			const value = e.target.value;
			this.confirmDirty = this.confirmDirty || !!value;
		}
	}
};
</script>

<style scoped></style>
